<?php
$prefix = "";
if (isset($_GET['ajax'])) {
    session_start();
    $prefix = "../";
}

include_once $prefix . 'controller/registrasi.php';
include_once $prefix . 'controller/login.php';
include_once $prefix . 'controller/follow.php';
include_once $prefix . 'controller/admin.php';
include_once $prefix . 'controller/wall.php';
?>
<?php
if (isset($_GET['name_viewed'])!= NULL) {
    $name_viewed = $_GET['name_viewed'];
}
else if (isset($_SESSION['user_name'])!= NULL) {
    $name_viewed = $_SESSION['user_name'];
}
if (isset($_SESSION['user_name'])!= NULL) {
    $name_logon = $_SESSION['user_name'];
    $temp = new login();
    $role = $temp->getRole($name_logon );
}
else {
    $name_logon = NULL;
    $role = NULL;
}
?>
<?php
$tesWall = new wall();
$tesFollow = new follow();
$log = new login();

if ($log->getWallPrev($name_viewed) == 'owner') {
    header("location:profil.php?name_viewed=".$name_viewed);
}else if ($log->getWallPrev($name_viewed) == 'follower_user') {
    if ($name_logon == $name_viewed) {

    }else if (!($tesFollow->isFollowing($name_viewed, $name_logon))) {
        header("location:profil.php?name_viewed=".$name_viewed);
    }
}

if(isset ($_POST['wall_post'])) {
    $tesWall->sentWall($name_viewed, $name_logon, $_POST['wall_post']);
}

if  (isset ($_GET['deleteWallID']) != NULL) {
    $tesWall->deleteWall($_GET['deleteWallID']);
}

if (isset ($_GET['commentWallID']) != NULL) {
    $tesWall->sentWallCom($_GET['commentWallID'], $name_logon, $_POST['body_comment_wall']);
}

if  (isset ($_GET['deleteWallComID']) != NULL) {
    $tesWall->deleteWallCom($_GET['deleteWallComID']);
}

?>
<?php
?>
<div id ="content" class="isipage">
    <?php
    $prof = $log->showProfileByUserName($name_logon);
    $resultPict = mysql_fetch_array($prof);
    $urlProfPict = $resultPict['url_photo'];
    ?>
    <div class="isipagestandar">
        <div class="tempatfotowall">
            <div class="profPictUserWall">
                <img src="<?php echo $urlProfPict;?>" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100" border="0"/>
            </div>
            <!--<div class="nameUserWall">
                <label id="bold"><?php echo $name_logon;?></label>
            </div>-->

        </div>
        <div class="tempatpostwall">
            <div>
                <label id="titlebold">Wall Comment:</label>
            </div>
            <div>
                <form  action="<?php echo 'wall.php?name_viewed='.$name_viewed;?>" method="post">
                    <div>
                        <textarea id="photocommentarea" name="wall_post" cols="45" rows="5"></textarea>
                    </div>
                    <button type="submit" id="commentbutton" name="submit_button">Post</button>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="isipage">
    <div class="isipagestandar">
        <div>
            <div>
                <label id="titlebold"> Recent Walls: </label>
            </div>
            <div class="space"></div>
            <?php
            $listWall = new wall();
            $resultWall = $listWall->showWall($name_viewed);
            $iterate=0;
            $numWall = mysql_num_rows($resultWall);
            while($row = mysql_fetch_array($resultWall)) {
                $iterate++;
                $userCom = $row['user_name_post'];
                $bodyCom = $row['wall_body'];
                $userWall = new login();
                $url = $userWall->getUrlPhoto($userCom);
                ?>
            <div class="boxListWall">
                <div class="profPictWall"> <img src="<?php echo $url;?>" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100" border="0"/> </div>
                <label class="labelinput"> <?php echo $userCom;?> wrote on <?php echo $row['post_date']. ' '.$row['post_time'];?></label>
                <label class="input" id="bold"><?php echo $bodyCom;?></label>
                <div class="deleteWall">
                        <?php
                        if ($name_logon == $row['user_name_post']) {
                            echo'<a href="wall.php?name_viewed='.$name_viewed.'&deleteWallID='.$row['wall_id'].'"><img src="images/delete.png" alt="deleteWallButton" height="30" width="75" border="0"/></a>';
                        }
                        ?>
                </div>
                <div class="boxListCommentWall">
                    <div class="listCommentWall">
                            <?php
                            $resultWallCom = $listWall->showWallCom($row['wall_id']);
                            if (mysql_num_rows($resultWallCom) > 0) {
                                echo '<label id="bold"> Recent Wall Comments: </label>';
                            }
                            while ($rowComWall = mysql_fetch_array($resultWallCom)) {
                                $userComWall = $rowComWall['from'];
                                $bodyComWall = $rowComWall['wall_com_body'];
                                $url = $userWall->getUrlPhoto($userComWall);
                                ?>
                        <div class="profPictComWall"> <img src="<?php echo $url;?>" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="70" height="70" border="0"/> </div>
                        <div class="userComWall"> <?php echo $userComWall;?> commented on <?php echo $row['post_date']. ' '.$row['post_time'];?></div>
                        <div class="bodyComWall" id="bold"><?php echo $bodyComWall;?> </div>
                        <div class="deleteWallCom">
                                    <?php
                                    if ($name_logon == $rowComWall['from']) {
                                        echo'<a href="wall.php?name_viewed='.$name_viewed.'&deleteWallComID='.$rowComWall['wall_com_id'].'"><img src="images/delete.png" alt="deleteWallComButton" height="30" width="75" border="0"/></a>';
                                    }
                                    ?>
                        </div>
                        <div class="space"></div>
                                <?php } ?>


                    </div>
                    <div class="commentWall">
                            <?php
                            if ((isset ($_GET['addCommentWallID'])) && ($row['wall_id'] == $_GET['addCommentWallID'])) {
                                ?>
                        <div class="space">

                        </div>

                        <div class="profPictUserWallComment">
                            <img src="<?php echo $urlProfPict;?>" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100" border="0"/>
                        </div>
                        <div class="nameUserWallComment">
                            <label id="nama_wall"><?php echo $name_logon;?></label>
                        </div>
                        <div class="form_comment_wall">
                            <form  action="<?php echo 'wall.php?name_viewed='.$name_viewed.'&commentWallID='.$row['wall_id'];?>" method="post">
                                <div>
                                    <textarea id="photocommentarea" name="body_comment_wall" cols="50" rows="5 "></textarea>
                                </div>
                                <button type="submit" id="commentbutton" name="submit_button">Post</button>
                            </form>
                        </div>
                                <?php } else {?>
                        <label for="add comment">&nbsp;</label>
                        <div class="link" id="link_comment_wall">
                                    <?php echo'<a href="wall.php?name_viewed='.$name_viewed.'&addCommentWallID='.$row['wall_id'].'"><img src="images/comment.png" alt="commentWallButton" height="30" width="80" border="0"/></a>'; ?>
                        </div>
                                <?php } ?>
                    </div>
                    <div class="space"></div>
                </div>
            </div>
                <?php } ?>
        </div>
    </div>
</div>